package com.axend.aerosense.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.axend.aerosense.user.fragment.UserCentryFragment;
import com.axend.aerosense.user.fragment.UserSettingStep1Fragment;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.impl.IComplete;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.databinding.UserActivityDevSettingBinding;

public class UserSettingActivity extends MvvmBaseActivity<UserActivityDevSettingBinding , BaseViewModel> {
    public final static String OPEN_TYPE = "OPEN_TYPE";
    private int type = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        type = getIntent().getIntExtra(OPEN_TYPE, 0);
        setSupportActionBar(viewDataBinding.userToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());
        initData();
        initViews();

    }

    @Override
    protected void onResume() {
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        super.onResume();
    }

    private void initViews() {
        Fragment fragment = null;
        if(0 == type){
            fragment = UserSettingStep1Fragment.getInstance();
        }else if(1 == type){
            fragment = UserCentryFragment.getInstance();
        }

        FragmentUtil.add(this,
                getContentId(),
                fragment,
                null,
                false);

        viewDataBinding.userToolbar.setMySettingText("");
        viewDataBinding.userToolbar.setSettingTextOnClickListener(v->{
            Object object = getSupportFragmentManager().findFragmentById(getContentId());
            if(object instanceof IComplete){
                ((IComplete) object).complete();
            }
        });
    }

    public void setRightTitle(String title){
        if(TextUtils.isEmpty(title)){
            viewDataBinding.userToolbar.setMySettingText("");
        }else{
            viewDataBinding.userToolbar.setMySettingText(title);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(null != this.getCurrentFocus()){
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() == 0){
            finish();
            return;
        }
//        FragmentUtils.show(getSupportFragmentManager());
        super.onBackPressed();
    }

    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                FragmentManager manager = getSupportFragmentManager();
                if (manager != null) {
                    Fragment currFrag = (Fragment) manager.findFragmentById(getContentId());
                    currFrag.onResume();
                }
            }
        };

        return result;
    }

    public int getContentId() {
        return viewDataBinding.userContent.getId();
    }

    public void setTitle(String title){
        viewDataBinding.userTitle.setText(title);
    }

    private void initData() {
        
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.user_activity_dev_setting;
    }

    @Override
    protected void onRetryBtnClick() {

    }
}
